package com.lutzyt.delivery.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lutzyt.delivery.bean.*;
import com.lutzyt.delivery.service.HorsemanAccountFlowService;
import com.lutzyt.delivery.service.HorsemanCommentService;
import com.lutzyt.delivery.service.HorsemanService;
import com.lutzyt.delivery.service.OrderService;
import com.lutzyt.delivery.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * controller 控制器
 *
 * @author zyt
 * @date 2021-05-18
 */
@Slf4j
@RestController
@RequestMapping("/api/horseman")
public class HorsemanController extends ActionController<HorsemanService> {
  private HorsemanService service;
  private OrderService orderService;
  private HorsemanAccountFlowService flowService;
  private HorsemanCommentService commentService;

  @PostMapping("/init")
  public Result<Void> init(@RequestBody Horseman customer) {
    log.info("customer:{}",customer);
    customer.setStatus(1);
    service.updateById(customer);
    return Result.success();
  }

  @GetMapping("/{id}")
  public Result<Horseman> getById(@PathVariable Integer id) {
    final Horseman bean = service.getById(id);
    return Result.success(bean);
  }
  @GetMapping("/{id}/order")
  public Result<List<Order>> order(@PathVariable Integer id){
    List<Order> orders = orderService.findByHorsemanId(id);
    return Result.success(orders);
  }

  @GetMapping("/{id}/comment")
  public Result<List<HorsemanComment>> comment(@PathVariable Integer id){
    List<HorsemanComment> orders = commentService.findByHorsemanId(id);
    return Result.success(orders);
  }


  @GetMapping("/{id}/flow")
  public Result<List<HorsemanAccountFlow>> flow(@PathVariable Integer id){
    List<HorsemanAccountFlow> flows = flowService.findByHorsemanId(id);
    return Result.success(flows);
  }

  @PutMapping("/{id}/takeIn/order/{orderId}")
  public Result<Void> takeIn(@PathVariable String id, @PathVariable String orderId){
    orderService.takeIn(id, orderId);
    return Result.success();
  }

  @PutMapping("/{id}/withdraw")
  public Result<Void> withdraw(@PathVariable String id){
    service.withdraw(id);
    return Result.success();
  }

  @PutMapping("/{id}")
  public Result<Void> update(@RequestBody Horseman bean) {
    log.info("更新菜单:{}", bean);
    service.update(bean);
    return Result.success();
  }

  @PostMapping("/page")
  public Result<Page<Horseman>> page(Integer pageSize, Integer pageNum) {
    final Page<Horseman> page = service.page(new Page<>(pageNum, pageSize));
    return Result.success(page);
  }

  @DeleteMapping("/{id}")
  public Result<Void> del(@PathVariable Integer id) {
    log.info("删除:{}", id);
    service.deleteById(id);
    return Result.success();
  }

  @DeleteMapping
  public Result<Void> del(@RequestParam("id") List<String> ids) {
    log.info("pl删除:{}", ids);
    service.removeByIds(ids);
    return Result.success();
  }

  @PostMapping
  public Result<Void> insert(@RequestBody Horseman bean) {
    log.info("添加菜单:{}", bean);
    service.insert(bean);
    return Result.success();
  }


  @Autowired
  public void setCommentService(HorsemanCommentService commentService) {
    this.commentService = commentService;
  }

  @Autowired
  public void setFlowService(HorsemanAccountFlowService flowService) {
    this.flowService = flowService;
  }

  @Autowired
  public void setOrderService(OrderService orderService) {
    this.orderService = orderService;
  }

  @Autowired
  public void setService(HorsemanService service) {
    this.service = service;
  }
}
    